package com.wm.web.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Scriptorium;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.AreaService;
import com.wm.service.CityService;
import com.wm.service.ScriptoriumService;
import com.wm.service.UserService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.servlet.UserCookie;
import com.wm.web.servlet.UserSession;

public class GetResAction extends BaseAction implements SessionAware{

	private static final Log logger = LogFactory.getLog(GetResAction.class);
	
	private Map session;

	public void setSession(Map session){
    	this.session = session;
    }
	
	private ScriptoriumService scriptoriumService;

	public ScriptoriumService getScriptoriumService() {
		return scriptoriumService;
	}

	public void setScriptoriumService(ScriptoriumService scriptoriumService) {
		this.scriptoriumService = scriptoriumService;
	}
	
	public String q;
	public String jsonString;
	
	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
	
	private PageList pageList;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	
	
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception ex) {
			logger.error(ex);
			return ERROR;
		}
	}
	
	public String index() {
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("");
		this.setPageList(this.getScriptoriumService().findScriptoriumsByScrNameCityIdAreaIdIsShow(q, -1, -1, 1, "scrInitials", Constant.ORDER_ASC, pages));
		if(this.getPageList()!=null && this.getPageList().getObjectList()!=null){
			this.setJsonString("[");
			for(int i=0;i<this.getPageList().getObjectList().size();i++){
				Scriptorium s = (Scriptorium)this.getPageList().getObjectList().get(i);
				if(i>0){
					this.setJsonString(this.getJsonString()+",{\"name\":\""+s.getScrName()+"\",\"id\":\""+s.getId()+"\"}");
				}else{
					this.setJsonString(this.getJsonString()+"{\"name\":\""+s.getScrName()+"\",\"id\":\""+s.getId()+"\"}");
				}
			}
			this.setJsonString(this.getJsonString()+"]");
//			this.setJsonString("[{\"name\":\"有色大厦\",\"id\":\"1\"}]");
			System.out.println(this.getJsonString());
		}
		return "index";
	}
	
}
